<template>
  <div class="container">
    <el-dialog
      style="height: 100%"
      :visible="showDialog"
      :title="showTitle"
      width="500px"
      @close="cancelBtn"
    >
      <el-form
        ref="addForm"
        label-width="80px"
        :model="formData"
        :rules="rules"
      >
        <el-form-item v-if="showSub" label="所属学科" prop="subjectName">
          <el-select
            v-model="formData.subjectID"
            placeholder="请选择"
            @change="selectChange"
          >
            <el-option
              v-for="item in tableData"
              :key="item.id"
              :label="item.subjectName"
              :value="item.subjectID"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="目录名称" prop="tagName">
          <el-input
            v-model="formData.tagName"
            placeholder="请输入目录名称"
          />
        </el-form-item>
        <el-row type="flex" justify="center">
          <el-button @click="cancelBtn">取消</el-button>
          <el-button type="primary" @click="btnOk">确认</el-button>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { add, update, detail } from '@/api/hmmm/tags'
import { list } from '../../api/hmmm/subjects'
export default {
  props: {
    tableData: {
      type: Array,
      // eslint-disable-next-line vue/require-valid-default-prop
      default: []
    },
    showDialog: {
      type: Boolean,
      default: false
    },
    id: {
      // type: { Number, String }
      type: String,
      default: ''
    }
  },
  data() {
    return {
      formData: {
        subjectName: '',
        subjectID: '',
        tagName: ''
      },
      rules: {
        tagName: [
          { required: true, message: '目录名称不能为空', trigger: 'blur' }
        ]
      },
      subjectID: null
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '修改标签' : '新增标签'
    },
    showSub() {
      return !this.$route.query.id
    }
  },
  created() {
    if (!this.$route.query.id) { this.getAddList() }
  },
  mounted() {
    console.log(this)
  },
  methods: {
    // 获取学科列表
    async getAddList() {
      const { data } = await list()
      // console.log(data);
      this.options = data.items
      // console.log(this.options.subjectName);
    },
    //  添加
    btnOk() {
      this.$refs.addForm.validate(async(isOK) => {
        if (isOK) {
          if (this.formData.id) {
            await update(this.formData)
          } else {
            // 表示可以提交
            if (!this.showSub) {
              // cs
              await add({ tagName: this.formData.tagName, subjectID: +this.id })
            } else {
              await add({ ...this.formData, subjectID: this.subjectID })
            }

            // this.getAddList()
          }
          this.$message.success(`添加成功`)
          // 同样，在新增成功之后，调用告诉父组件，重新拉取数据
          this.$emit('addSubject')
          // 子组件 update:固定写法 (update:props名称, 值)
          this.$emit('update:showDialog', false) // 触发事件
        }
      })
    },

    // 取消按钮
    cancelBtn() {
      this.formData = {
        // 重置表单
        subjectName: '',
        tagName: ''
      }
      this.$refs.addForm.resetFields() // 重置校验结果
      this.$emit('changeid', null)
      this.$emit('update:showDialog', false)
    },
    selectChange(val) {
      this.subjectID = val
    },

    // 获取详情
    async getDetail(row) {
      // console.log(row);
      const { data } = await detail(row)
      console.log(data)
      this.formData = data
      // console.log(this.formData)
    }
  }
}
</script>

<style scoped lang='less'></style>
